package org.test.client.ui.component.progress;

import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Paint;

import javax.swing.JFrame;
import javax.swing.JPanel;

import org.test.client.ui.util.Icons;
import org.test.client.ui.util.UIUtil;

public class MyProgressBar extends JPanel{
	private Color blackBorderColor1 = Icons.getColor("000000");
	private Color blackBorderColor2 = Icons.getColor("26291d");
	private Color blakBackColor1 = Icons.getColor("0f0e0e");
	private Color blakBackColor2 = Icons.getColor("171717");
	
	//水平渐变
	private Color greenBarColor1 = Icons.getColor("8fba20");
	private Color greenBarColor2 = Icons.getColor("cfe54c");
	private Color greenBarBorder = Icons.getColor("c5dd53");
	
	private int diameter = 20;
	
	
	private int progress = 30;
	
	private int prew = 45;
	private int preh = 405;
	
	public MyProgressBar(int progress){
		this.progress = progress;
		this.setOpaque(false);
	}
	
	
	@Override
	protected void paintComponent(Graphics g) {

		Graphics2D g2d = (Graphics2D)g;
		UIUtil.setAntiAliasing(g2d, true);
		int width = this.getWidth();
		int height = this.getHeight();

		diameter = height;
		if(diameter % 2 != 0){
			diameter = diameter-1;
		}
		int x = 0;
		int y = 0;
		int w = width-1;
		int h = height-1;
		//1.画黑 border
		g2d.setColor(blackBorderColor1);
		g2d.drawRoundRect(x, y, w, h, diameter, diameter);
		
		x = x+1;
		y= y + 1;
		w = w-2;
		h = h-2;
		diameter = diameter -2;
		g2d.setColor(blackBorderColor2);
		g2d.drawRoundRect(x, y, w, h, diameter, diameter);
		
		
		//2.画黑back
		Paint p1 = UIUtil.getPaint(blakBackColor1, blackBorderColor2, 0, 0, 0, h);
		g2d.setPaint(p1);
//		x = x +1;
//		y = y+1;
//		w = w-1;
//		h = h-1;
		diameter = diameter -2;
		g2d.fillRoundRect(x, y, w, h, diameter, diameter);
		
		
		//3.画进度条高亮
		int x2 = x + 2;
		int y2 = y + 2;
		int w2 = w - 5;
		w2 = (int) ((double)w2*((double)progress/(double)100));
		int h2 = h - 5;
		g2d.setColor(greenBarBorder);
		diameter = diameter - 4;
		g2d.drawRoundRect(x2, y2, w2, h2, diameter, diameter);
		
		//4.画绿进度条
		x2 = x2 + 1;
		y2 = y2 + 1;
		w2 = w2 -1;
		h2 = h2 -1;
		diameter = diameter - 2;
		Paint p2 = UIUtil.getPaint(greenBarColor1, greenBarColor2, 0, 0, w2, 0);
		g2d.setPaint(p2);
		g2d.fillRoundRect(x2, y2, w2, h2, diameter, diameter);
	
		//5.画进度条 shadow线
		
		
		//6.画进度条 底部shadow
		
		
		//7.画进度条头部高亮
		
	}
	
	@Override
	protected void paintBorder(Graphics g) {
		
	}
	
	public static void main(String[] args) {
		
		JFrame frame = new JFrame();
		JPanel jPanel = new JPanel();
		jPanel.setLayout(null);
		Container contentPane = frame.getContentPane();
		contentPane.add(jPanel);
		
//		jPanel.setBackground(Color.black);
		
		MyProgressBar mp = new MyProgressBar(70);
		jPanel.add(mp);
		mp.setBounds(10, 50, 320, 30);
		
		frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
		frame.setSize(450, 300);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}
	
}
